package com.workManager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workManager.mapper.TbProcurementEstimateMapper;
import com.workManager.mapper.TbProcurementMapper;
import com.workManager.pojo.*;
import com.workManager.util.DateUtil;
import com.workManager.util.WebUtil;
import com.workManager.vo.ProcurementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by worso on 2017/3/21 0021.
 */
@Service
public class ProcurementServiceImpl implements ProcurementService {

    @Autowired
    private TbProcurementMapper procurementMapper;

    @Autowired
    private TbProcurementEstimateMapper procurementEstimateMapper;

    public int add(TbProcurement procurement) {
        return procurementMapper.insert(procurement);
    }

    public int delete(List<String> Ids) {
        for(String id:Ids){
            TbProcurement procurement = procurementMapper.selectByPrimaryKey(Integer.valueOf(id));
            procurementMapper.deleteByPrimaryKey(Integer.valueOf(id));

            TbProcurementEstimateExample example = new TbProcurementEstimateExample();
            example.createCriteria().andProcurementIdEqualTo(procurement.getProcurementId());
            List<TbProcurementEstimate> list = procurementEstimateMapper.selectByExample(example);
            if(list.size()>0){
                TbProcurementEstimate procurementEstimate = list.get(0);
                Map map = new HashMap();
                map.put("remark", procurementEstimate.getRemark()+"|["+ DateUtil.getDate()+":"+ WebUtil.getCurrentUserVO().getUserName()+"]:已删除实际采购，报废");
                map.put("id", procurementEstimate.getId());
                procurementEstimateMapper.invalid(map);
            }

        }
        return Ids.size();
    }

    public int update(TbProcurement procurement) {
        return procurementMapper.updateByPrimaryKey(procurement);
    }

    public PageInfo<ProcurementVo> query(Map map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);
        List<ProcurementVo> procurementList = procurementMapper.query(map);
        PageInfo<ProcurementVo> procurementPageInfo = new PageInfo<ProcurementVo>(procurementList);
        return procurementPageInfo;
    }
}
